package j20251122;

public class Demo1 {
    // 要构建的复杂产品
    static class Computer {
        private String CPU;
        private String RAM;
        // ... 更多部件
        // 私有构造器，只能通过Builder构建
        private Computer(Builder builder) {
            this.CPU = builder.CPU;
            this.RAM = builder.RAM;
        }
        // 静态内部类 Builder
        public static class Builder {
            private String CPU;
            private String RAM;
            public Builder setCPU(String CPU) {
                this.CPU = CPU;
                return this;
            }
            public Builder setRAM(String RAM) {
                this.RAM = RAM;
                return this;
            }
            public Computer build() {
                return new Computer(this);
            }
        }
    }

    public static void main(String[] args) {
        // 使用
        Computer computer = new Computer.Builder()
                .setCPU("Intel i7")
                .setRAM("16GB")
                .build();
    }
}
